/**
 * @project_name staff_vehicle
 * @package_name com.ssh.dao
 * @desc TODO
 * @param
 * @author jack
 * @date 2013-4-25
 * @time 下午3:24:54
 */
package com.ssh.dao;

import java.util.List;

import org.hibernate.SessionFactory;

import com.ssh.entity.DLicence;
import com.ssh.entity.InnerDriver;

/**
 * @author jack
 *
 */
public class InnerDriverDAOImpl implements InnerDriverDAOI {
	private SessionFactory sessionFactory;

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerDriverDAOI#queryById(java.lang.String)
	 */
	@Override
	public InnerDriver queryById(String id) {
		// TODO Auto-generated method stub
		InnerDriver innerDriver ;
		try{
			String hql = "from InnerDriver as i  where i.staffId=?";
			innerDriver =
					(InnerDriver) sessionFactory.getCurrentSession().
					createQuery(hql).setString(0, id).uniqueResult();
		}
		catch(Exception e){
			return null;
		}

		return innerDriver ;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerDriverDAOI#updateByInnerDriver(com.ssh.entity.InnerDriver)
	 */
	@Override
	public InnerDriver updateByInnerDriver(InnerDriver innerDriver) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().update(innerDriver);
		}
		catch(Exception e){
			return null ;
		}

		return innerDriver;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerDriverDAOI#deleteById(java.lang.String)
	 */
	@Override
	public int deleteById(String id) {
		// TODO Auto-generated method stub
		int num ;
		try{
			String hql = "delete InnerDriver as i where i.staffId=?";
			num = sessionFactory.getCurrentSession().createQuery(hql).setString(0, id).executeUpdate();
		}
		catch(Exception e){
			return 0 ;
		}
		return num;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerDriverDAOI#addInnerDriver(com.ssh.entity.InnerDriver)
	 */
	@Override
	public InnerDriver addInnerDriver(InnerDriver innerDriver) {
		// TODO Auto-generated method stub
		try{
			sessionFactory.getCurrentSession().save(innerDriver);
		}
		catch(Exception e){
			return null;
		}
		return innerDriver ;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerDriverDAOI#addInnerDriverList(java.util.List)
	 */
	@Override
	public int addInnerDriverList(List<InnerDriver> innerDriverList) {
		// TODO Auto-generated method stub
		int length = innerDriverList.size();
		try{
			for(int i=0;i<length;i++)
				addInnerDriver(innerDriverList.get(i));
		}
		catch(Exception e){
			return 0;
		}
		return length;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerDriverDAOI#queryByIdList(java.lang.String)
	 */
	@Override
	public List<InnerDriver> queryByIdList(String strings) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see com.ssh.dao.InnerDriverDAOI#countById(java.lang.String)
	 */
	@Override
	public int countById(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * @return the sessionFactory
	 */
	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	/**
	 * @param sessionFactory the sessionFactory to set
	 */
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<InnerDriver> queryByState(String state) {
		// TODO Auto-generated method stub
		try{
			String hql = "from InnerDriver as i where i.state=:state";
			return sessionFactory.getCurrentSession().createQuery(hql).setParameter("state", state).list();
		}
		catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}

}
